package com.future.network.receiver

import android.net.ConnectivityManager
import android.net.LinkProperties
import android.net.Network
import android.net.NetworkCapabilities
import android.os.Build
import androidx.annotation.RequiresApi
import com.future.network.NetworkType
import com.future.network.NetWorkMonitorManager
import com.future.network.NetworkManager
import com.future.network.utiles.NetworkUtils


@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
class NetworkCallbackImpl : ConnectivityManager.NetworkCallback() {

    /**
     * 当访问指定的网络被阻止或解除阻塞时调用
     */
    override fun onBlockedStatusChanged(network: Network, blocked: Boolean) {
        super.onBlockedStatusChanged(network, blocked)
    }

    /**
     * 当网络状态修改但仍旧是可用状态时调用
     */
    override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
        super.onCapabilitiesChanged(network, networkCapabilities)
        // 表明此网络连接成功验证
        if (networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)) {
            if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)
                || networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI_AWARE)
            ) {
                // 使用WI-FI
                if (NetworkUtils.isFastClick(200)) {//在某些手机上WiFi和流量同时开启的时候会触发多次
                    NetWorkMonitorManager.get().post(NetworkType.NETWORK_WIFI)
                    NetworkManager.get().post(NetworkType.NETWORK_WIFI)
                }
            } else if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) ||
                networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) {
                // 使用蜂窝网络
                NetWorkMonitorManager.get().post(NetworkType.NETWORK)
                NetworkManager.get().post(NetworkType.NETWORK)
            } else {
                // 未知网络，包括蓝牙、VPN、LoWPAN
                NetWorkMonitorManager.get().post(NetworkType.NETWORK_UNKNOWN)
                NetworkManager.get().post(NetworkType.NETWORK_UNKNOWN)
            }
        }
    }

    /**
     * 网络断开
     */
    override fun onLost(network: Network) {
        super.onLost(network)
        NetWorkMonitorManager.get().post(NetworkType.NETWORK_NO)
        NetworkManager.get().post(NetworkType.NETWORK_NO)
    }

    /**
     * 当网络连接的属性被修改时调用
     */
    override fun onLinkPropertiesChanged(network: Network, linkProperties: LinkProperties) {
        super.onLinkPropertiesChanged(network, linkProperties)
    }

    /**
     *
     * 网络正在减弱，链接会丢失数据，即将断开网络时调用
     */
    override fun onLosing(network: Network, maxMsToLive: Int) {
        super.onLosing(network, maxMsToLive)
        NetWorkMonitorManager.get().post(NetworkType.ONLOSTING)
        NetworkManager.get().post(NetworkType.ONLOSTING)
    }

    /**
     * 当网络连接超时或网络请求达不到可用要求时调用
     */
    override fun onUnavailable() {
        super.onUnavailable()
    }

    /**
     * 网络连接成功，通知可以使用的时候调用
     */
    override fun onAvailable(network: Network) {
        super.onAvailable(network)
        NetWorkMonitorManager.get().post(NetworkType.RECONNECTION)
        NetworkManager.get().post(NetworkType.RECONNECTION)
    }
}